module.exports = {
	time(value, type) {
		console.log(value);
		value = parseInt(value)
		// console.log(value,'时间戳');
		let date = new Date(value);
		let y = date.getFullYear();
		let MM = date.getMonth() + 1;
		MM = MM < 10 ? "0" + MM : MM;
		let d = date.getDate();
		d = d < 10 ? "0" + d : d;
		let h = date.getHours();
		h = h < 10 ? "0" + h : h;
		let m = date.getMinutes();
		m = m < 10 ? "0" + m : m;
		let s = date.getSeconds();
		s = s < 10 ? "0" + s : s;
		if (type == 'YMD') {
			return y + "-" + MM + "-" + d;
		}
		if (type == 'YMDHMS') {
			return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
		}
		if (type == 'YMDHM') {
			return y + "-" + MM + "-" + d + " " + h + ":" + m;
		}
		if (type == 'YMDH') {
			return y + "-" + MM + "-" + d + " " + h;
		}
		return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
	},

	// 查看大图
	/**
	 * 参数一，要查看的图片数组，单个可直接传地址，【Array,String】
	 * 参数二，如果是数组需要查看的当前位置下标
	 */
	previmg(data, legnth = 0) {
		console.log('图', data)
		console.log('下标', legnth);
		var arr = []
		if (typeof data == 'object') {
			arr = [...data]
		} else {
			arr.push(data)
		}

		uni.previewImage({
			current: legnth,
			urls: arr || '',
		})
	},
	// 返回上一页
	navigateBack() {
		var pages = getCurrentPages(); //当前页
		if (pages.length < 2) {
			uni.reLaunch({
				url: '/pages/index/index'
			})
			return
		}
		uni.navigateBack({
			delta: -2
		});
	},
	getNavWHdata() {
		console.log('获取导航信息');
		try {
			// 有时候getSystemInfoSync API在手机端会报错 所以我做了缓存，
			const value = uni.getStorageSync('statusBar_key_hk');
			if (value) {
				let obj = {
					statusBarHeight: value.statusBarHeight,
					navBarHeight: value.navBarHeight,
					windowWidth: value.windowWidth,
				}
				console.log('获取缓存导航信息成功');
				this.$store.dispatch('addasync', obj)
				return
			}


			let obj = {}
			// 获取手机系统信息
			const info = uni.getSystemInfoSync()
			// 设置状态栏高度（H5顶部无状态栏小程序有状态栏需要撑起高度）
			obj.statusBarHeight = info.statusBarHeight
			obj.windowWidth = info.windowWidth

			// 获取胶囊的位置
			const menuButtonInfo = uni.getMenuButtonBoundingClientRect()
			// console.log(menuButtonInfo);
			// (胶囊底部高度 - 状态栏的高度) + (胶囊顶部高度 - 状态栏内的高度) = 导航栏的高度
			obj.navBarHeight = (menuButtonInfo.bottom - info.statusBarHeight) + (menuButtonInfo.top - info
				.statusBarHeight)
			obj.windowWidth = menuButtonInfo.left
			this.$store.dispatch('addasync', obj)
			uni.setStorage({
				key: 'statusBar_key_hk',
				data: obj,
				success: function() {
					console.log('缓存导航信息成功');
				}
			});
			return

		} catch (e) {
			console.log(e);
			//TODO handle the exception
			uni.showToast({
				title: '获取导航信息出错',
				icon: 'none'
			})
		}
	},
	// 判断登录态是否过期
	codeStatus() {
		const value = uni.getStorageSync('token_tiem');
		if (value) {
			let tiemState = (Date.now() - value.constTiem) / 1000
			if (tiemState >= value.exp_sec) {
				uni.removeStorageSync('token_tiem');
				uni.removeStorageSync('hk_token');
				try {
					uni.clearStorageSync();
				} catch (e) {
					// error
				}
				uni.showModal({
					title: '提示',
					content: '登录已过期，请重新登录',
					showCancel: false,
					success: (res) => {
						if (res.confirm) {
							uni.reLaunch({
								url: '/pages/login/login'
							})
						}
					}
				});
			} else {

				// 获取用户信息
				this.userMD.getUserData.apply(this)
			}

		}
	},
	// 提示框
	showToast(title, icon) {
		uni.showToast({
			title,
			icon: icon ? '' : 'none'
		})
	},
	// 保存图片到手机
	xiazai(e) {
		console.log(e,'图片图片');
		wx.downloadFile({
		      url: e,//图片地址
		      success: function (res) {
		        //图片保存到本地
		        wx.saveImageToPhotosAlbum({
		          filePath: res.tempFilePath,
		          success: function (data) {
		            wx.hideLoading()
					uni.showToast({
						title:'保存成功'
					})
		          },
		          fail: function (err) {
		            if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail authorize no response") {
		              // 这边微信做过调整，必须要在按钮中触发，因此需要在弹框回调中进行调用
		              wx.showModal({
		                title: '提示',
		                content: '需要您授权保存相册',
		                showCancel: false,
		                success: modalSuccess => {
		                  wx.openSetting({
		                    success(settingdata) {
		                      console.log("settingdata", settingdata)
		                      if (settingdata.authSetting['scope.writePhotosAlbum']) {
		                        wx.showModal({
		                          title: '提示',
		                          content: '获取权限成功,再次点击保存',
		                          showCancel: false,
		                        })
		                      } else {
		                        wx.showModal({
		                          title: '提示',
		                          content: '获取权限失败，将无法保存到相册哦~',
		                          showCancel: false,
		                        })
		                      }
		                    },
		                    fail(failData) {
		                      console.log("failData", failData)
		                    },
		                    complete(finishData) {
		                      console.log("finishData", finishData)
		                    }
		                  })
		                }
		              })
		            }
		          },
		          complete(res) {
		            wx.hideLoading()
		          }
		        })
		      }
		    })
	},
	// 拨打电话
	bdPhoen(e){
		if(!e){
			uni.showToast({
				title:'请输入手机号码',
				icon:'none'
			})
			return
		}
		uni.makePhoneCall({
		    phoneNumber: e
		});
	},
	// 获取用户当前定位
	getPositionInfo(){
		return new Promise((resoLve,resject)=>{
			uni.getLocation({
			    type: 'wgs84',
			    success: res=>{
			        resoLve(res)
			    },
				fail:(e)=>{
			    	resject(e)
			    }
			});
		})
	},
	// 打开地图选择位置
	chooseLocation(){
		return new Promise((resoLve,resject)=>{
			uni.chooseLocation({
			    success:res=>{
					resoLve(res)
				},
				fail:(e)=>{
					resject(e)
				}
			});
		})
	}
}
